﻿using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using ToolUtity;
namespace AHCS
{
    public class Common
    {
        public static string EditTuLi = "";
        public static int saveday = 0;

        public static int hasback = 0;
        public static Color BackColor = Color.Chartreuse;
        public static bool hasNetWork = false;



        /// <summary>
        /// 获取对应的字号
        /// </summary>
        /// <param name="str">小一</param>
        /// <returns></returns>
        public static float GetFontSizeByName(string str)
        {
            float res = 10;
            switch (str)
            {
                case "初号":
                    res = 42;
                    break;
                case "小初":
                    res = 36;
                    break;
                case "一号":
                    res = 26;
                    break;
                case "小一":
                    res = 24;
                    break;
                case "二号":
                    res = 22;
                    break;
                case "小二":
                    res = 18;
                    break;
                case "三号":
                    res = 16;
                    break;
                case "小三":
                    res = 15;
                    break;
                case "四号":
                    res = 14;
                    break;
                case "小四":
                    res = 12;
                    break;
                case "五号":
                    res = 10.5f;
                    break;
                case "小五":
                    res = 9;
                    break;
                case "六号":
                    res = 7.5f;
                    break;
                case "小六":
                    res = 6.5f;
                    break;
                default:
                    res = 5;
                    break;
            }
            return res;
        }


        //字符串中的数字加1
        public static string StrNumAdd(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            StringBuilder sb = new StringBuilder();

            string dig = "";
            foreach (char item in str)
            {
                if (CharOpera.isDigit(item))
                {
                    dig += item;
                }
                else
                {
                    if (dig != "")
                    {
                        sb.Append((dig.ToInt32() + 1).ToString());
                        dig = "";
                    }
                    sb.Append(item);
                }

            }
            return sb.ToString(); ;
        }

        public static string CorrectString(Match match)
        {
            string matchValue = match.Value;
            matchValue = matchValue.Replace("（", "").Replace("）", "");
            return matchValue;
        }


       /// <summary>
       /// word转PDF，同名文件PDF
       /// </summary>
       /// <param name="sourcePath"></param>
       /// <returns></returns>
        public static bool WordToPDF(string sourcePath)
        {
            string fname= Path.GetFileNameWithoutExtension(sourcePath)+".pdf";
            string dir= Path.GetDirectoryName(sourcePath);
            string pdfname = Path.Combine(dir, fname);
            return WordToPDF(sourcePath, pdfname);
        }

        /// <summary>
        /// 很多电脑不行不用这个
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="targetPath"></param>
        /// <returns></returns>
        public static bool WordToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Document document = null;
            try
            {

                application.Visible = false;
                document = application.Documents.Open(sourcePath);
                document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
                result = true;
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
               // Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                document.Close();
            }
            return result;
        }

        /// <summary>
        /// 传入DOC名字，保存同文件夹pdf
        /// </summary>
        /// <param name="filesrc"></param>
        /// <returns></returns>
        public static bool ASPOSEWordToPDF(string filesrc )
        {
            string fname = Path.GetFileNameWithoutExtension(filesrc) + ".pdf";
            string dir = Path.GetDirectoryName(filesrc);
            string pdfname = Path.Combine(dir, fname);

            try
            {
               Aspose.Words.Document doc = new Aspose.Words.Document(filesrc);
                doc.Save(pdfname);
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
            
            }
           
            return true; ;
        }

        /// <summary>
        /// 导出到指定路径
        /// </summary>
        /// <param name="docname"></param>
        /// <param name="pdfname"></param>
        /// <returns></returns>
        public static bool ASPOSEWordToPDF(string docname , string pdfname)
        { 
            try
            {
                Aspose.Words.Document doc = new Aspose.Words.Document(docname);
                doc.Save(pdfname);
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);

            }

            return true; ;
        }


      
	

        public static DateTime GetNetTime(out bool isok)
        {
            isok = false;

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://www.baidu.com/");
                webRequest.Method = "GET";
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                string st = webResponse.Headers["Date"];
                DateTime wdt = DateTime.Parse(st);
                // MessageBox.Show("网络时间：" + wdt.ToString("yyyy-MM-dd HH:mm:ss") +        "系统时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                webResponse.Close();
                webResponse.Dispose();
                //   res = Convert.ToInt32(wdt.ToString("yyyyMMdd"));
                isok = true;
                return wdt;
            }
            catch (Exception)
            {

                return DateTime.Now;
            }

        }

        public static void WriteLog(string msg)
        {

            string currentDirectory = Environment.CurrentDirectory;
            string sFileName = currentDirectory + @"\运行日志.txt";
            FileStream fs;
            StreamWriter sw;
            if (File.Exists(sFileName))
            //验证文件是否存在，有则追加，无则创建
            {
                fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
            }
            else
            {
                fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
            }
            sw = new StreamWriter(fs);
            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "   ---   " + msg);
            sw.Close();
            fs.Close();
        }


        public static bool ISkeyong()
        {
            string zcb = TimeClass.ReadSetting("saveDay", "");

            if (zcb != "")
            {
                string strriqi = TimeClass.ReadSetting("saveDay", "");
                string riqi = Encryption.DisEncryPW(strriqi); ; //判断 注册表 有时间

                bool isok = true;
                 DateTime nettime = Common.GetNetTime(out isok);
              
                //DateTime nettime = DateTime.Parse("2022-08-24");
                if (isok == false)
                {
                    hasNetWork = false;
                 //   MessageBox.Show("网络连接断开，注意及时保存内容，请在24小时内联网，否则软件自动退出");
                    nettime = DateTime.Now;
                    //Environment.Exit(0);
                }
                else
                {
                    hasNetWork = true;
                    //TimeClass.WriteSetting("runMcount", "1");//     清空计时器
                }

                DateTime d1 = DateTime.Parse(riqi.ToString());

                var dd = d1 - nettime;
                Common.saveday = dd.Days;
                if (Common.saveday <= 0)
                {

                    return false; 
                }
                else
                {
                    return true;
                }
              
            }
            return false;
        }
    }
}
